package collector

import (
	"context"
	"fmt"

	"github.com/go-redis/redis/v8"
)

// GetAllFromHash connects to the Redis server and retrieves all fields and values from a hash key.
func GetAllFromHash(redisUrl, redisPassword, hashKey string) (map[string]string, error) {
	// Create a new Redis client with options
	options := &redis.Options{
		Addr:     redisUrl,
		Password: redisPassword,
	}

	client := redis.NewClient(options)
	defer client.Close()

	// Create a context
	ctx := context.Background()

	// Perform HGETALL operation
	result, err := client.HGetAll(ctx, hashKey).Result()
	if err != nil {
		return nil, fmt.Errorf("failed to get data from Redis: %w", err)
	}

	return result, nil
}
